package com.block.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.db.entity.ProductBaseInfo;
import com.block.db.mapper.ProductBaseInfoMapper;
import com.block.product.service.IProductBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 产品基本信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-09-02
 */
@Service
public class ProductBaseInfoServiceImpl extends ServiceImpl<ProductBaseInfoMapper, ProductBaseInfo> implements IProductBaseInfoService {

    @Resource
    private RedisTemplate<String, ProductBaseInfo> redisProductBaseInfoTemplate;

    private  static  final  String PRODUCTINFO_KEY_PREFIX="PRODUCT:BASEINFO:";
    private  static  final  Long PRODUCTINFO_KEY_TIME_OUT=4L;

    @Override
    public ProductBaseInfo getProductInfoById(Integer productId){
        String redisKey = PRODUCTINFO_KEY_PREFIX+productId;
        ProductBaseInfo productBaseInfo= redisProductBaseInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(productBaseInfo)){
            productBaseInfo = this.baseMapper.selectById(productId);
            if(productBaseInfo!=null) {
                redisProductBaseInfoTemplate.opsForValue().set(redisKey, productBaseInfo, PRODUCTINFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return productBaseInfo;
    }


}
